﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Text;
using com.stockstar.baseinfo.datacontract;
using com.stockstar.baseinfo.datacontract.param;

public partial class tab_fundfav : System.Web.UI.Page
{
    protected int errno = 0;
    protected string errmsg = "";
    public StringOper sop = new StringOper();
    protected Dictionary<string, FundNetValueReport> mainDate = new Dictionary<string, FundNetValueReport>();

    protected FundNetValueReport[] netValues = new FundNetValueReport[] { };
    BaseInfoFundCom.FundCom fundoper = new BaseInfoFundCom.FundCom();

    protected int col = 6;
    protected int sortflag = 1;

    private string fundcodes = "";

    public struct FundInfo
    {
        public FundNetValueReport data;
        public string fundMsg;
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        //HttpCookie mycookie = Request.Cookies["fundfavlist"];
        //if (Request["code"] != null)
        //{
        //    saveCookie(Request["code"]);
        //}
        //if (Request["action"] == "del" && Request["code"] != null)
        //{
        //    delfavfund(mycookie.Value.ToString(), Request["code"].ToString());
        //    //Response.Redirect("fundfav.aspx");
        //}

    }
    private void saveCookie(string fundcode)
    {
        if (!sop.chkFundCode(fundcode))
        {
            errno = 1002;//所选基金不合法
            errmsg = "暂无符合条件的基金，请重新输入";
            return;
        }
        string fundstr = "";
        if (Request.Cookies["fundfavlist"] != null)
        {
            fundstr = Request.Cookies["fundfavlist"].Value;
        }
        if (fundstr.IndexOf(fundcode + "_") >= 0)
        {
            errno = 1001;//所选基金已存在
            errmsg = "所选基金已存在";
            return;
        }
        if (fundstr.Split('_').Length >= 21)
        {
            errmsg = "对不起，您只能保存20条自选基金。";
            return;
        }
        else
        {
            fundstr = fundcode + "_" + fundstr;//新添加的基金排列在前面
            //updateCookie(fundstr);
            //Response.Redirect("fundfav.aspx");
            GotoNewURL(fundstr);
        }

    }

    private void GotoNewURL(string fundstr)
    {
        //double random = new Random().NextDouble();
        //string js = "<script type=\"text/javascript\""
        //       + ">var the_date = new Date(\"December 31, 2060\");var expiresDate = the_date.toGMTString();"
        //       + "document.cookie = \"fundfavlist=" + fundstr + ";expires=\" + expiresDate + \";path=/;domain=fund.stockstar.com\""
        //           + ";top.location.href = \"/fav/tab/fundfav.aspx\";</script>";
        string js = "<script type=\"text/javascript\""
       + ">var the_date = new Date(\"December 31, 2060\");var expiresDate = the_date.toGMTString();"
       + "document.cookie = \"fundfavlist=" + fundstr + ";expires=\" + expiresDate"
            + ";top.location.href = \"fundfav.aspx\";</script>";
        //Page.ClientScript.RegisterStartupScript(Page.GetType(), "fundcodejs", js);
        Response.Write(js);
    }

    //private void updateCookie(string fundstr)
    //{
    //    //Response.Cookies.Remove("fundfavlist");
    //    HttpCookie mycookis = new HttpCookie("fundfavlist");
    //    mycookis.Value = fundstr;
    //    mycookis.Expires = DateTime.Today.AddYears(50);
    //    mycookis.Path = "/";
    //    //mycookis.Domain = "fund.stockstar.com";
    //    //Response.SetCookie(mycookis);
    //    Response.Cookies.Add(mycookis);
    //    //Server.Transfer("fundfav.aspx");
    //}


    public void saveCache()
    {
        mainDate.Clear();
        ParamBase param = new ParamBase();
        param.investtype = -1;
        List<FundNetValueReport> tmpTb = fundoper.GetFundNetValueReportList(param);
        foreach (FundNetValueReport fundcode in tmpTb)
        {
            mainDate.Add(fundcode.FundCode.Code, fundcode);
        }
    }


    public void delfavfund(string fundcode, string delcode)
    {
        string[] codes = fundcode.Split('_');
        StringBuilder newfundcode = new StringBuilder();
        foreach (string item in codes)
        {
            if (!item.Equals(delcode) && !item.Equals(""))
            {
                newfundcode.Append(item);
                newfundcode.Append("_");
            }
        }
        GotoNewURL(newfundcode.ToString());
        //updateCookie(newfundcode.ToString());
    }

}
